<template>
  <div class="box">
    <div class="left">
      <a-tabs v-model:activeKey="textOrImageType" class="textOrImageTabs">
        <a-tab-pane key="text">
          <template #tab>
            <span>
              <FontSizeOutlined />
              文本
            </span>
          </template>
          <components-list
            :list="templateTexts"
            :typeComponent="'text'"
            @on-item-click="handleItemClick"
          />
          <img id="test-img" />
        </a-tab-pane>
        <a-tab-pane key="image">
          <template #tab>
            <span>
              <FileImageOutlined />
              图片
            </span>
          </template>
          <components-list
            :list="templateImages"
            typeComponent="image"
            @on-item-click="handleItemClick"
          />
        </a-tab-pane>
      </a-tabs>
    </div>
    <div class="center">
      <div class="canvas">
        <div class="editdocter-header">
          <p>画布区域</p>
          <HistoryReOrUndo :style="{ position: 'absolute', right: '0px' }" />
        </div>

        <div class="hp" id="canvas-area" :class="{ 'canvas-fix': canvasFix }">
          <div class="editdocter-container" :style="pageProps">
            <EditorWrapper
              v-for="item in componentList"
              :key="item.id"
              :id="item.id"
              @set-active="setActive"
              @change="changeWrapperMove"
              :active="item.id === CurrentComponent?.id"
              :hidden="item.isHidden"
              :props="item.props"
              :style="{ zIndex: item.zIndex }"
            >
              <component
                :is="item.name"
                v-bind="item.props"
                :draggable="false"
              ></component>
            </EditorWrapper>
          </div>
        </div>
      </div>
    </div>
    <div class="right">
      <a-tabs v-model:activeKey="editorActiveKey" type="card">
        <a-tab-pane :key="EditdocterTabEnum.COMPNOENTS" tab="属性设置">
          <template v-if="CurrentComponent">
            <EditorGroup
              v-if="!CurrentComponent.isLocked"
              @change="changeElement"
              :props-list="CurrentComponent.props as AllComponentProps"
            />
            <a-empty v-else description="该元素已被锁定，无法编辑" />
          </template>

          <a-empty v-else description="在画布中选择元素并开始编辑" />
        </a-tab-pane>
        <a-tab-pane :key="EditdocterTabEnum.LAYER" tab="图层设置">
          <LayerList
            :current-layeri-id="CurrentComponent && CurrentComponent.id"
            :layer-list="componentList"
            @selctLayerIdChange="selctLayerIdChangeHandle"
            @selectLayerInfoChange="selectLayerInfoChangeHandle"
          />
        </a-tab-pane>
        <a-tab-pane :key="EditdocterTabEnum.PAGE" tab="页面设置">
          <PropsTable :props="pageProps" @change="changePagePropsHandle" />
        </a-tab-pane>
      </a-tabs>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, computed, ref, onMounted } from 'vue'
import useEditorStore from '../../store/modules/editorStore'
import type { ComponentData } from '../../store/modules/type/editor'
import {
  defaultTextTemplates,
  defaultImageTemplates,
} from '../../defaultTemplate'
import initHotKeys from '@/plugins/hotKeys'
import initContextMenu from '@/plugins/contextMenu'
import ComponentsList from '../../components/ComponentList.vue'
import EditorWrapper from '../../components/EditorWrapper.vue'
import PropsTable from '../../components/PropsTable.vue'
import { EditdocterTabEnum } from './editdocterType'
import LayerList from '@/components/LayerList/layer-list.vue'
import EditorGroup from '@/components/editor-group/editor-group.vue'
import type { AllComponentProps, IPageProps } from '@/defaultProps'
import { cloneDeep, pickBy } from 'lodash-es'
import HistoryReOrUndo from './components/history-reOrundo.vue'
import { useRoute } from 'vue-router'
import { FontSizeOutlined, FileImageOutlined } from '@ant-design/icons-vue'
import useSaveWork from '@/hooks/useSaveWork'
import useGlobalStore from '@/store/modules/globalStore'
export default defineComponent({
  name: 'EditDoctor',
  components: {
    ComponentsList,
    EditorWrapper,
    PropsTable,
    LayerList,
    EditorGroup,
    HistoryReOrUndo,
    FontSizeOutlined,
    FileImageOutlined,
  },
  setup() {
    const route = useRoute()
    const editorStore = useEditorStore()
    const globalStore = useGlobalStore()
    initHotKeys()
    initContextMenu()
    useSaveWork()
    // initPageClose()
    const componentList = computed<ComponentData[]>(() => {
      return editorStore.testComponents
    })
    const pageProps = computed(() => {
      return editorStore.pageData.props
    })
    const imgurl = ref('')
    const upload = ref()
    const textOrImageType = ref('text')
    const editorActiveKey = ref<EditdocterTabEnum>(EditdocterTabEnum.COMPNOENTS)
    const canvasFix = computed(() => {
      return globalStore.canvasFix
    })
    const CurrentComponent = computed(() => {
      return editorStore.CurrentComponent
    })
    const handleItemClick = (item: any) => {
      editorStore.addComponent(item)
    }
    const deleteElement = (id: string) => {
      editorStore.removeElement(id)
    }
    const setActive = (id: string) => {
      editorStore.setActiveComponent(id)
    }
    const changeElement = (data: any) => {
      editorStore.changeCurrentComponentProps(data)
    }
    const handleSuccess = (data: any) => {
      imgurl.value = data.res.data
    }
    const selctLayerIdChangeHandle = (id: string) => {
      editorStore.setActiveComponent(id)
    }
    const selectLayerInfoChangeHandle = (data: {
      data: { key: string; value: string }
      isRoot: boolean
    }) => {
      editorStore.changeCurrentComponentProps(data.data, data.isRoot)
    }
    const changePagePropsHandle = (data: {
      key: keyof IPageProps
      value: any
    }) => {
      editorStore.changePageData(data)
    }
    const changeWrapperMove = (data: any) => {
      const updateData = pickBy(data, (v, k) => k !== 'id')
      const keysArr = Object.keys(updateData)
      const valuesArr = Object.values(updateData).map((v) => v + 'px')
      editorStore.changeMovePosition(
        {
          key: keysArr,
          value: valuesArr,
        },
        data.id
      )
    }
    const getWorkDetailHandle = () => {
      const workId = route.params?.id ?? ''
      editorStore.getTemplateDetail(workId as string)
    }
    const templateTexts = cloneDeep(defaultTextTemplates)
    const templateImages = cloneDeep(defaultImageTemplates)
    onMounted(() => {
      getWorkDetailHandle()
      editorStore.resetEditorStore()
      editorStore.setWorkId((route.params?.id as string) ?? '')
    })
    return {
      componentList,
      templateTexts,
      templateImages,
      handleItemClick,
      deleteElement,
      setActive,
      CurrentComponent,
      changeElement,
      upload,
      handleSuccess,
      imgurl,
      editorActiveKey,
      EditdocterTabEnum,
      LayerList,
      selctLayerIdChangeHandle,
      selectLayerInfoChangeHandle,
      EditorGroup,
      pageProps,
      changePagePropsHandle,
      changeWrapperMove,
      HistoryReOrUndo,
      textOrImageType,
      FontSizeOutlined,
      FileImageOutlined,
      canvasFix,
    }
  },
})
</script>

<style scoped lang="scss">
.box {
  width: 100%;
  min-height: 85vh;
  display: flex;
  height: 100%;
  background-color: white;
  overflow: auto;
  .left {
    // flex: 2;
    padding: 20px;
    display: flex;
    width: 310px;
    height: 100vh;
    flex-direction: column;
    align-items: center;
    overflow: auto;

    .textOrImageTabs {
      width: 100%;
    }
  }
  .center {
    flex: 6;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding-top: 30px;
    position: relative;
    background-color: rgba(235, 235, 235);
    .canvas {
      display: flex;
      flex-direction: column;
      align-items: center;
      width: 100%;
      .editdocter-header {
        width: 100%;
        display: flex;
        justify-content: center;
        // height: 30px;
        align-items: center;
        position: relative;
        p {
          opacity: 0.8;
        }
      }
      .hp {
        width: 375px;
        min-height: 200px;
        background-color: #ffffff;
        margin: 20px 0px;
        overflow-y: auto;
        overflow-x: hidden;
        max-height: 80vh;
        position: fixed;

        &::-webkit-scrollbar {
          width: 5px;
          height: 5px;
        }
        &::-webkit-scrollbar-thumb {
          background-color: #dcdcdc;
          border-radius: 5px;
        }
        &::-webkit-scrollbar-track {
          background-color: #ffffff;
          border-radius: 5px;
        }
      }
      .canvas-fix .editdocter-container .edit-wrapper > * {
        box-shadow: none !important;
      }
      .canvas-fix {
        max-height: none;
        position: absolute;
      }
    }
  }
  .right {
    flex: 2;
    padding-top: 20px;
    overflow: auto;
    height: 100vh;
    background-color: white;
  }
  :deep(.ant-tabs) {
    height: 100%;
  }
  :deep(.ant-tabs-content) {
    height: 100%;
  }
}
</style>
